package com.xvxingan.mvc.mq.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.camel.Body;
import org.apache.camel.Consume;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import com.xvxingan.utils.DateUtil;
import com.xvxingan.utils.EncryptUtil;

@Service
public class TestService {
	@Produce(uri="jms:topic:test")
	public  ProducerTemplate producer;
	
	@Consume(uri="jms:queue:VirtualTopicConsumers.yace.test?concurrentConsumers=20")
	public void consume(@Body long i){
		System.out.println("收到消息 index:"+i);
  		Map<String,String> map = new HashMap<String,String>();
		map.put("partner", "CNDCP-WAREHOUSE-KEY");
		map.put("sign_type", "MD5");
		map.put("notify_type", "tms_order_notify");
		String notify_id = EncryptUtil.md5(i+"").toUpperCase();
 		String content = "<request> <order_code>{{1}}</order_code> <tms_service_code>DAYUPEI-0015</tms_service_code> <tms_order_code>{{2}}</tms_order_code> <receiver_name>桃子</receiver_name> <receiver_zip>000000</receiver_zip> <receiver_province>北京</receiver_province> <receiver_city>北京市</receiver_city> <receiver_district>西城区</receiver_district> <receiver_address>红莲南里居委会</receiver_address> <receiver_mobile>18066666666</receiver_mobile> <package_weight>7.0</package_weight> <wms_code>STORE_192237</wms_code> <wms_address>测试地址001</wms_address> <item_list> <item> <item_quantity>1</item_quantity> <insuredAmount>250000</insuredAmount> <extendFields>is_precious:1</extendFields> <item_name>***</item_name> </item> <item> <item_quantity>1</item_quantity> <insuredAmount>600000</insuredAmount> <extendFields>is_precious:1</extendFields> <item_name>***</item_name> </item> </item_list> <totalInsuredAmount>850000</totalInsuredAmount> </request>";
		content = content.replace("{{1}}", i+"");
		content = content.replace("{{2}}", i+"");
		String sign = null;
		try {
			sign = EncryptUtil.encryptBASE64(EncryptUtil.md5(content+"haier123456").toLowerCase().getBytes());
		} catch (Exception e1) {
 			e1.printStackTrace();
		}
		map.put("notify_time", DateUtil.dateFormat());
		map.put("content", content);
		map.put("notify_id", notify_id);
		map.put("sign", sign);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost("http://120.26.94.83/njcbdmp/tmall/interface");
		//HttpPost method = new HttpPost("http://localhost:8080/dmp/tmall/interface");
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		Iterator<String> it = map.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			NameValuePair pair = new BasicNameValuePair(key, map.get(key));
			list.add(pair);
		}
		try {
			method.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
 			e1.printStackTrace();
		}
		HttpResponse result = null;
		try {
			result = httpClient.execute(method);
		} catch (ClientProtocolException e1) {
 			e1.printStackTrace();
		} catch (IOException e1) {
 			e1.printStackTrace();
		}
		if (result.getStatusLine().getStatusCode() == 200) {
			try {
				String str = EntityUtils.toString(result.getEntity());
				System.out.println("响应："+str);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
